package com.gitee.feizns.quickstart.web.ex.impl;

import com.gitee.feizns.dynamic.Objs;
import com.gitee.feizns.dynamic.reflect.Classes;
import com.gitee.feizns.quickstart.web.ex.ExceptionHandler;

/**
 * 为自定异常处理器做的便捷写法
 * @see ExceptionHandler
 * @param <E>
 */
public abstract class AbstractExceptionHandler<E extends Throwable> implements ExceptionHandler<E> {

    /**
     * 支持处理的类型
     */
    private final Class<?>[] supportsClass;

    public AbstractExceptionHandler() {
        this.supportsClass = Classes.ofSuperclassGenericTypes(this.getClass());
    }

    @SafeVarargs
    public AbstractExceptionHandler(Class<E>... supportsClass) {
        this.supportsClass = Objs.isEmpty(supportsClass) ?
                Classes.ofSuperclassGenericTypes(this.getClass()) : supportsClass;
    }

    /**
     * 默认的情况直接使用继承该类时，标注的类型参数（E）
     */
    @Override
    @SuppressWarnings("unchecked")
    public Class<? extends Throwable>[] support() {
        return (Class<? extends Throwable>[]) supportsClass;
//        return (Class<? extends Throwable>[]) (ArrayUtils.isEmpty(supportsClass) ? ClassUtils.getSuperclassGenericTypes(this.getClass()) : supportsClass);
    }

}
